/*
 * Copyright (C) 2010  Sylvain Maucourt (smaucourt@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *
 */
package net.sylvek.where;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.FacebookException;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;

import java.util.concurrent.Executors;

public class UpdateStatus extends DlgActivity {

    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id) {
        default:
            return super.onCreateDialog(id);
        case ASK_DLG:
            final String message = getIntent().getExtras().getString(Intent.EXTRA_TEXT);
            return new AlertDialog.Builder(this).setTitle(R.string.share_on_facebook)
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton(android.R.string.ok, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(UpdateStatus.this);
                            final String accessToken = pref.getString(Where.STORED_ACCESS_TOKEN, null);
                            if (accessToken == null) {
                                return;
                            }
                            Executors.newCachedThreadPool().execute(new Runnable() {

                                @Override
                                public void run()
                                {
                                    try {
                                        Looper.prepare();
                                        FacebookClient fbClient = new DefaultFacebookClient(accessToken);
                                        fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", message));
                                    } catch (FacebookException e) {
                                        Toast.makeText(UpdateStatus.this, getText(R.string.publish_error), Toast.LENGTH_LONG)
                                                .show();
                                        Log.e("update.status", e.getMessage());
                                    } finally {
                                        Looper.loop();
                                    }
                                }
                            });
                            finish();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            finish();
                        }
                    })
                    .create();
        }
    }
}
